package cn.bigfire.compiler.dragon.appendixA.handwrite.inter;

import cn.bigfire.compiler.dragon.appendixA.handwrite.lexer.Token;
import cn.bigfire.compiler.dragon.appendixA.handwrite.symbols.Type;

public class Logical extends Expr {

    public Expr expr1, expr2;

    public Logical(Token token, Expr expr1, Expr expr2) {
        super(token, null);
        this.expr1 = expr1;
        this.expr2 = expr2;
        type = check(expr1.type, expr2.type);
    }

    public Type check(Type type1, Type type2) {
        if (type1 == Type.Bool && type2 == Type.Bool) {
            return Type.Bool;
        }else {
            return null;
        }
    }

    public Expr gen(){
        int f = newLabel();
        int a = newLabel();
        Temp temp = new Temp(type);
        this.jumping(0, f);
        emit(temp.type + " = true");
        emit("goto L" + a);
        emitLabel(f);
        emit(temp.toString() + " = false");
        emitLabel(a);
        return temp;
    }

    @Override
    public String toString() {
        return expr1.toString() + " " + token.toString() + " " + expr2.toString();
    }

}
